package YS;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import HZW.add;

public class App extends JFrame implements ActionListener, MouseListener {

	// 词库表的数据库操作类（对词库进行增删改查）
	private BaseDao bDao = new BaseDao();
	// 单词表的数据库操作类（对单词进行增删改查）
	private WordDao wDao = new WordDao();

	// 装词库的Map集合 将词库信息从数据库查询出来后，以键值对的形式将词库的ID和词库名称存进map集合中
	private Map<Integer, String> baseMap = new HashMap<>();

	private JLabel lblTip;
	private JComboBox<String> comboBox;
	private JPanel panelTop, panelBottom;
	private JButton btnEnter;
	private JMenuBar menuBar;// 菜单栏
	private JMenu menu1, menu2; // 菜单项
	private JMenuItem menuItem11, menuItem21;

	private JPopupMenu mainMenu;// 右键弹出菜单
	private JMenuItem refreshMenu;

	public App() {

		super("我爱记单词v1.0");
		this.setLayout(null);

		menuBar = new JMenuBar();

		menu1 = new JMenu("系统管理");
		menuItem11 = new JMenuItem("词库管理");
		menuItem11.addActionListener(this);

		menu1.add(menuItem11);
		menuBar.add(menu1);

		menu2 = new JMenu("帮助");
		menuItem21 = new JMenuItem("关于词典");
		menuItem21.addActionListener(this);

		menu2.add(menuItem21);
		menuBar.add(menu2);

		this.setJMenuBar(menuBar);

		lblTip = new JLabel("请选择词库：");
		lblTip.setFont(new Font("", Font.BOLD, 20));
		lblTip.addMouseListener(this);

		comboBox = new JComboBox<String>();
		comboBox.setFont(new Font("", Font.BOLD, 20));
		try {
			// 从数据库中取数据
			List<Base> bases = bDao.getBases();

			// 装进map集合中
			if (bases != null && bases.size() > 0) {
				for (Base b : bases) {
					baseMap.put(b.getBaseid(), b.getBasename());
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		// 从map集合中取出 key对应的值（为了方便根据词库名称找到词库ID） ，放进下拉框控件
		for (Integer key : baseMap.keySet()) {
			comboBox.addItem(baseMap.get(key));
		}

		panelTop = new JPanel();
		panelTop.add(lblTip);
		panelTop.add(comboBox);

		panelTop.setBounds(20, 60, 300, 60);
		this.add(panelTop);

		btnEnter = new JButton("开始记单词");

		panelBottom = new JPanel();
		btnEnter.setFont(new Font("", Font.BOLD, 20));
		btnEnter.addActionListener(this);
		panelBottom.add(btnEnter);
		panelBottom.setBounds(40, 140, 300, 60);

		this.add(panelBottom);

		this.setSize(400, 300);
		this.setLocationRelativeTo(null); // 设置窗口相对于指定组件的位置
		this.setResizable(false); // 禁止改变窗体大小
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	// public static void main(String[] args) {
	// new App();
	// }

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == menuItem21) { // 关于菜单
			new About();
		} else if (e.getSource() == menuItem11) { // 词库管理菜单
			// new BaseList();
			new add();
		} else if (e.getSource() == btnEnter) { // 开始记单词

			if (comboBox.getItemCount() == 0) {
				JOptionPane.showMessageDialog(null, "词库为空！请新建词库");
				return;
			}

			if (comboBox.getSelectedIndex() == -1) {
				JOptionPane.showMessageDialog(null, "抱歉！你还没有选择词库");
				return;
			}
			// 获取词库
			String selectedValue = (String) comboBox.getSelectedItem();
			int key = MapUtil.getKey(baseMap, selectedValue);

			try {
				List<Word> words = wDao.getWords(key);
				if (words.size() < 10) {
					JOptionPane.showMessageDialog(null,
							"当前词库的词量不足10个，请先添加单词或者切换词库！");
					return;
				}
				// this.setVisible(false);
				new ReciteWord(key);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == refreshMenu) {
			this.setVisible(false);
			new App();
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == lblTip) {
			mainMenu = new JPopupMenu();// 自己添加menu的Item

			refreshMenu = new JMenuItem("刷新列表");
			refreshMenu.addActionListener(this);

			mainMenu.add(refreshMenu);
			mainMenu.show(lblTip, e.getX(), e.getY());
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}
}
